package com.cn.ysf.newbeemall.service.Impl;

import com.cn.ysf.newbeemall.controller.vo.NewBeeMallIndexConfigGoodsVO;
import com.cn.ysf.newbeemall.mapper.NewBeeMallGoodsMapper;
import com.cn.ysf.newbeemall.mapper.TbNewbeeMallIndexConfigMapper;
import com.cn.ysf.newbeemall.pojo.TbNewbeeMallGoodsInfo;
import com.cn.ysf.newbeemall.pojo.TbNewbeeMallIndexConfig;
import com.cn.ysf.newbeemall.service.NewBeeMallIndexConfigService;
import com.cn.ysf.newbeemall.util.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ysf
 * @date 2019/12/3 18:32
 */
@Service
public class NewBeeMallIndexConfigServiceImpl implements NewBeeMallIndexConfigService {

    @Resource
    private TbNewbeeMallIndexConfigMapper tbNewbeeMallIndexConfigMapper;

    @Resource
    private NewBeeMallGoodsMapper newBeeMallGoodsMapper;

    @Override
    public List<NewBeeMallIndexConfigGoodsVO> getConfigGoodsesForIndex(Long configType, Long number) {
        List<NewBeeMallIndexConfigGoodsVO> newBeeMallIndexConfigGoodsVOS= new ArrayList<NewBeeMallIndexConfigGoodsVO>(Math.toIntExact(number));
        List<TbNewbeeMallIndexConfig> indexConfigs=tbNewbeeMallIndexConfigMapper.findIndexConfigsByTypeAndNum(configType,number);
        if(!CollectionUtils.isEmpty(indexConfigs)){
            //取出所有的商品ids
            List<Long> goodIds=indexConfigs.stream().map(TbNewbeeMallIndexConfig::getGoodsId).collect(Collectors.toList());
            List<TbNewbeeMallGoodsInfo> newbeeMallGoodsInfos=newBeeMallGoodsMapper.selectByPrimaryKeys(goodIds);

            newBeeMallIndexConfigGoodsVOS= BeanUtil.copyList(newbeeMallGoodsInfos,NewBeeMallIndexConfigGoodsVO.class);

            for(NewBeeMallIndexConfigGoodsVO newBeeMallIndexConfigGoodsVO:newBeeMallIndexConfigGoodsVOS){
                //商品名称
                String goodsName = newBeeMallIndexConfigGoodsVO.getGoodsName();
                //商品简介
                String goodsIntro = newBeeMallIndexConfigGoodsVO.getGoodsIntro();
                //字符串过长导致文字超出的问题
                if(goodsName.length()>30){
                    goodsName=goodsName.substring(0,30)+"...";
                    newBeeMallIndexConfigGoodsVO.setGoodsName(goodsName);
                }
                if(goodsIntro.length()>22){
                    goodsIntro=goodsIntro.substring(0,22)+"...";
                    newBeeMallIndexConfigGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        return newBeeMallIndexConfigGoodsVOS;
    }
}
